const service = require("../../../../service/port.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    unpayList: [],
    status: "1", //状态,1、未付款 2、已付款,未发货 3、已发货,未确认 4、交易成功5、交易关闭 6、已评价7、退货中 8、退货成功,9退货失败
    pageSize: 4,
    pageNum: 1,
    noData: false, // 是否有更多数据
    noDatas: false, //一条数据都没有
    cancelOrdershow: false, //取消订单 默认不显示
    radio: 1, //取消订单
    currentstatus: "", //取消订单 公用
    orderId: "", //取消订单 公用
    canclevalue: "", //取消原因
    statusText:"",//状态描述
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getList(callback) {
    let customerId = wx.getStorageSync("userInfo").id;
    service.getTbOrderList(
      {
        tbOrder: {
          customerId: customerId, //客户id
          status: this.data.status,
        },
        queryRequest: {
          pageSize: this.data.pageSize,
          pageNum: this.data.pageNum,
        },
      },
      (res) => {
        for (let i = 0; i < res.data.rows.length; i++) {
          const element = res.data.rows[i];
          if(element.timelyDelivery==1){
           
            this.setData({
              statusText:"待付款"
            })
          }else if(element.timelyDelivery==2){
            if(element.postStatus==1){
              this.setData({
                statusText:"未配送"
              })
            }else if(element.postStatus==2){
              this.setData({
                statusText:"配送中"
              })
            }else if(element.postStatus==3){
              this.setData({
                statusText:"已配送"
              })
            }else{
              this.setData({
                statusText:"配送异常"
              })
            }

          }else{
            this.setData({
              statusText:"到店自取"
            })
          }
          
        }

        callback(res);
      }
    );
  },
  onLoad: function () {
    var self = this;
    // 订单列表
    wx.showLoading({
      title: "加载中...",
    });
    this.getList(function (res) { 
      console.log(res)
      if (res.data.total == 0) {
        self.setData({
          noDatas: true,
          noData:false
        });
      } else if (res.data.total > 0 && res.data.total < 4) {
        self.setData({          
          noData: true,         
        });
      } 
      self.setData({
        unpayList: res.data.rows,
      });
      wx.hideLoading();
    });
  },
  //跳转到订单详情
  productdetail: function (e) {
    console.log(e);
    var index = e.currentTarget.dataset.index.id;
    wx.navigateTo({
      url: "/pages/order/orderDetail/index?id=" + index,
    });
  },
  unpayTo: function (e) {
    wx.redirectTo({
      url: "/pages/mine/order/unpayList/index",
    });
  },
  unreviceTo: function (e) {
    wx.redirectTo({
      url: "/pages/mine/order/unreviceList/index",
    });
  },
  completeTo: function (e) {
    wx.redirectTo({
      url: "/pages/mine/order/completeList/index",
    });
  },
  closerTo: function (e) {
    wx.redirectTo({
      url: "/pages/mine/order/closerList/index",
    });
  },
  refundTo: function (e) {
    wx.redirectTo({
      url: "/pages/mine/order/refundList/index",
    });
  },
  //取消订单 begin
  cancelOrder: function (e) {
    console.log(e)
    this.setData({
      cancelOrdershow: true,
      currentstatus: e.currentTarget.dataset.index.status,
      orderId: e.currentTarget.dataset.index.id
    })
  },
  onChange(event) {

    this.setData({
      radio: event.detail,
    });
  },
  onClose: function () {
    this.setData({
      cancelOrdershow: false,
      radio:''
    })
  },
  onClick(event) {
    const {
      name
    } = event.currentTarget.dataset;
    this.setData({
      radio: name,
    });
  },
  getcanclevalue: function (e) {
    this.setData({
      canclevalue: e.detail.value,
    });
  },    
  cancleButton: function (e) {
      const self = this
      service.cancleTbOrder({
        "orderId": this.data.orderId, //订单标识
        "status": "5", //状态,1、未付款 2、已付款,未发货 3、已发货,未确认 4、交易成功5、交易关闭 6、已评价7、退货中 8、退货成功,9退货失败
        "reason": this.data.radio + ',' + this.data.canclevalue, //取消原因
      }, function (res) {
        self.onLoad()    
        wx.showToast({
          title: '已取消',
          icon: 'success',
          duration: 2000
        })
        self.setData({
          cancelOrdershow: false,
          radio: '',
          canclevalue: '',
          noData: false,        
        });
       
      

      })
    },
  //取消订单  end
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 请求数据
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      pageNum: 1,
      noData: false,
    });
    const self = this;
    this.getList(function (res) {
      if (res.data.rows.length > 0 && res.data.rows.length < 4) {
        self.setData({
          unpayList: res.data.rows,
          noData: true,
        });
      } else {
        self.setData({
          unpayList: res.data.rows,
        });
      }
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 判读数据是否加载完毕
    if (this.data.noData) return;
    // 停止下拉刷新
    wx.stopPullDownRefresh();
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    const unpayList = this.data.unpayList;
    let pageNum = this.data.pageNum;
    this.setData({
      // 每次下拉 page+1
      pageNum: ++pageNum,
    });
    const self = this;
    self.getList(function (res) {      
      if (res.data.rows.length == 0) {
        self.setData({
          noData: true,
        });
      } else{
        res.data.rows.forEach((item) => {
          unpayList.push(item);
        });
        self.setData({
          unpayList: unpayList,
        });
      }
    });
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
